package com.wen.tian.controller;

import com.wen.tian.bean.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 登录
 *
 * @author tww
 * ClassName: LoginController.java、
 * createTime: 2021年01月02日 15:54:56
 **/
@Controller
public class LoginController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @GetMapping("login")
    public String toLogin(String kickout, Model model) {
        model.addAttribute("kickout", kickout);
        return "login";
    }

    @PostMapping("login")
    @ResponseBody
    public Result toLogin(String userName, String passWord, String reme) {

        //获取主体
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new UsernamePasswordToken(userName, passWord));
            return success(200, "登录成功");
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return error(500, e.getMessage());
        }
    }
}
